package com.jyu.apts.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.jyu.apts.dto.ItemBatchRecordDto;
import com.jyu.apts.dto.ResultDto;
import com.jyu.apts.dto.TableDto;
import com.jyu.apts.dto.UserDto;
import com.jyu.apts.entity.ItemBatchRecord;
import com.jyu.apts.entity.ItemProductionLine;
import com.jyu.apts.entity.Organization;
import com.jyu.apts.entity.User;
import com.jyu.apts.entityEnum.ItemBatchRecordStatusEnum;
import com.jyu.apts.service.ItemBatchRecordService;
import com.jyu.apts.service.ItemProductionLineService;
import com.jyu.apts.service.QRCodeService;

@RestController
public class ItemBatchRecordController {

	@Autowired
	QRCodeService qrCodeService;
	@Autowired
	ItemBatchRecordService itemBatchRecordService;
	@Autowired
	ItemProductionLineService itemProductionLineService;
	
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/itemBatchRecord_html")
	public ModelAndView itemBatchRecord_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/itemBatchRecord.html");
		return mv;
	}
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/addItemBatchRecord")
	public ResultDto addItemBatchRecord(long count,long itemProductionLineId) {
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(itemProductionLineId);
		if(itemProductionLine == null) {
			return new ResultDto("生产线不存在",null,2);
		}
		ItemBatchRecord itemBatchRecord = new ItemBatchRecord();
		itemBatchRecord.setCount(count);
		itemBatchRecord.setItemProductionLine(itemProductionLine);
		itemBatchRecordService.save(itemBatchRecord);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/upItemBatchRecord")
	public ResultDto upItemBatchRecord(long id,long count,long itemProductionLineId) {
		ItemBatchRecord itemBatchRecord = itemBatchRecordService.getOne(id);
		if(itemBatchRecord == null) {
			return new ResultDto("商品批次不存在!",null,2);
		}
		ItemProductionLine itemProductionLine = itemProductionLineService.getOne(itemProductionLineId);
		if(itemProductionLine == null) {
			return new ResultDto("生产线不存在",null,2);
		}
		itemBatchRecord.setItemProductionLine(itemProductionLine);
		itemBatchRecord.setCount(count);
		itemBatchRecordService.save(itemBatchRecord);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/delItemBatchRecord")
	public ResultDto delItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<ItemBatchRecord> itemBatchRecords = new ArrayList<ItemBatchRecord>();
		for(String id : idArray) {
			ItemBatchRecord itemBatchRecord = new ItemBatchRecord();
			itemBatchRecord.setId(new Long(id));
			itemBatchRecords.add(itemBatchRecord);
		}
		itemBatchRecordService.delete(itemBatchRecords);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/stopItemBatchRecord")
	public ResultDto stopItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		List<ItemBatchRecord> itemBatchRecords = itemBatchRecordService.findAllById(l_ids);
		for(ItemBatchRecord itemBatchRecord : itemBatchRecords) {
			itemBatchRecord.setStatus(ItemBatchRecordStatusEnum.STOPED.getType());
		}
		itemBatchRecordService.save(itemBatchRecords);
		return new ResultDto("暂停成功",null,0);
	}
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/finishItemBatchRecord")
	public ResultDto finishItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		List<ItemBatchRecord> itemBatchRecords = itemBatchRecordService.findAllById(l_ids);
		for(ItemBatchRecord itemBatchRecord : itemBatchRecords) {
			itemBatchRecord.setStatus(ItemBatchRecordStatusEnum.FINISHED.getType());
		}
		itemBatchRecordService.save(itemBatchRecords);
		return new ResultDto("完成成功",null,0);
	}
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/workItemBatchRecord")
	public ResultDto workItemBatchRecord(String ids) {
		String[] idArray = ids.split(",");
		List<Long> l_ids = new ArrayList<Long>();
		for(String id : idArray) {
			l_ids.add(new Long(id));
		}
		List<ItemBatchRecord> itemBatchRecords = itemBatchRecordService.findAllById(l_ids);
		for(ItemBatchRecord itemBatchRecord : itemBatchRecords) {
			itemBatchRecord.setStatus(ItemBatchRecordStatusEnum.WORKING.getType());
		}
		itemBatchRecordService.save(itemBatchRecords);
		return new ResultDto("启动成功",null,0);
	}
	
	@RequiresPermissions("itemBatchRecord_html")
	@RequestMapping("/getItemBatchRecordPage")
	public TableDto<ItemBatchRecordDto> getItemBatchRecordPage(Integer pageNumber,Integer pageSize,Long itemProductionLineId,Integer status,String search) {
		Page<ItemBatchRecord> pageResult = itemBatchRecordService.getItemBatchRecordPage(pageNumber, pageSize, itemProductionLineId, status, search);
		List<ItemBatchRecord> itemBatchRecords = pageResult.getContent();
		List<ItemBatchRecordDto> itemBatchRecordDtos = new ArrayList<ItemBatchRecordDto>();
		itemBatchRecords.forEach(itemBatchRecord->itemBatchRecordDtos.add(new ItemBatchRecordDto().convert(itemBatchRecord)));
		return new TableDto<ItemBatchRecordDto>(itemBatchRecordDtos,pageResult.getTotalElements());
	}
	
}
